2020-2021 Sunseeker Telemetry and Lighting System
tec_ex2_extClearInput.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430F51x2 Demo - TimerD0/1-TEC, External Clear, Level trigger
34 //
35 // Description: This code example shows how to configure the TEC module for
36 // controlling the timerD PWM dutycycle from external clear input. In this
37 // example code, TD0 is used to generate the external clear pulses. TD1 is
38 // configired to output PWM of 25%, 50% and 75%. With the EXTCLR clear event
39 // on TD1 input, the TD1 timer counter (TDR) gets reset and this modifies the
40 // dutycycle of all the TD1 PWM channels (TD1.0, TD1.1, TD1.2)
41 // TD0 = 1KHz, 15% dutycycle; TD1 = 10kHz, 25%,50%,75% dutycycle
42 //
43 // Note: EXTCLR event in this case is High level on EXTCLR input. As long as,
44 // the input on EXTCLR is high, the timer counter will be held in reset
45 //
46 // ACLK = REFO = 32kHz; SMCLK = MCLK = DCO Clock = 12MHz;
47 //
48 // MSP430F51x2
49 // -----------------
50 // /|\| XIN|-
51 // | | |
52 // --|RST XOUT|-
53 // | |
54 // | P2.0/TD0.2|--> CCR2 -| ~15% duty cycle; External Clear Pulses
55 // | | |
56 // | P2.7/TEC1CLR|<---------| External Clear Input
57 // | |
58 // | P2.1/TD1.0|--> CCR0 - 50% dutycycle (Period/2)
59 // | P2.2/TD1.1|--> CCR1 - 25% dutycycle
60 // | P2.3/TD1.2|--> CCR2 - 75% dutycycle
61 //
62 // B. Nisarga
63 // Texas Instruments Inc.
64 // Nov 2010
65 // Built with CCS v4 and IAR Embedded Workbench Version: 4.21
66 //******************************************************************************
67 
68 #include "driverlib.h"
69 
70 //*****************************************************************************
71 //
72 //Target frequency for MCLK in kHz
73 //
74 //*****************************************************************************
75 #define UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ 24000
76 
77 //*****************************************************************************
78 //
79 //MCLK/FLLRef Ratio
80 //
81 //*****************************************************************************
82 #define UCS_MCLK_FLLREF_RATIO 374
83 
84 void main(void)
85 {
86  //Stop WDT
87  WDT_A_hold(WDT_A_BASE);
88 
89  // Increase Vcore setting to level1 to support fsystem=12MHz
90  // NOTE: Change core voltage one level at a time..
91  PMM_setVCore(PMMCOREV_1);
92 
93  // Configure DCO = 12Mhz
94  UCS_initClockSignal(UCS_FLLREF,
95  UCS_REFOCLK_SELECT,
96  UCS_CLOCK_DIVIDER_1
97  );
98 
99  UCS_initClockSignal(UCS_ACLK,
100  UCS_REFOCLK_SELECT,
101  UCS_CLOCK_DIVIDER_1
102  );
103 
104  UCS_initFLLSettle(UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ,
106  );
107 
108  // Configure TD0 port pins
109  GPIO_setAsPeripheralModuleFunctionOutputPin(
110  GPIO_PORT_P2,
111  GPIO_PIN0
112  );
113 
114  // Configure TimerD0 to combine CCR0/1 block to control TD0CCR2 PWM output
115  // TD0CCR2 PWM output = External clear pulses for TD1
116  // Freq = 1kHz, Period = 1ms; dutycycle = 15% => ON period = 150us
117  Timer_D_combineTDCCRToOutputPWMParam outputPWMParam = {0};
118  outputPWMParam.clockSource = TIMER_D_CLOCKSOURCE_ACLK;
119  outputPWMParam.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
120  outputPWMParam.clockingMode = TIMER_D_CLOCKINGMODE_EXTERNAL_CLOCK;
121  outputPWMParam.timerPeriod = 32;
122  outputPWMParam.combineCCRRegistersCombination = TIMER_D_COMBINE_CCR1_CCR2;
123  outputPWMParam.compareOutputMode = TIMER_D_OUTPUTMODE_RESET_SET;
124  outputPWMParam.dutyCycle1 = 20;
125  outputPWMParam.dutyCycle2 = 25;
126  Timer_D_combineTDCCRToOutputPWM(TIMER_D0_BASE, &outputPWMParam);
127 
128  Timer_D_startCounter(TIMER_D0_BASE,
129  TIMER_D_UP_MODE);
130 
131  // Configure TD1 port pins
132  GPIO_setAsPeripheralModuleFunctionOutputPin(
133  GPIO_PORT_P2,
134  GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3
135  );
136 
137 
138  // Configure TimerD1 to 10kHz with PWMs dutycycles = 25%, 50%, 75%
139  Timer_D_initUpModeParam initUpParam = {0};
140  initUpParam.clockSource = TIMER_D_CLOCKSOURCE_SMCLK;
141  initUpParam.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
142  initUpParam.clockingMode = TIMER_D_CLOCKINGMODE_EXTERNAL_CLOCK;
143  initUpParam.timerPeriod = 1200;
144  initUpParam.timerInterruptEnable_TDIE = TIMER_D_TDIE_INTERRUPT_DISABLE;
145  initUpParam.captureCompareInterruptEnable_CCR0_CCIE =
146  TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
147  initUpParam.timerClear = TIMER_D_DO_CLEAR;
148  Timer_D_initUpMode(TIMER_D1_BASE, &initUpParam);
149 
150  Timer_D_initCompareModeParam initComp0Param = {0};
151  initComp0Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_0;
152  initComp0Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
153  initComp0Param.compareOutputMode = TIMER_D_OUTPUTMODE_TOGGLE;
154  initComp0Param.compareValue = 1200;
155  Timer_D_initCompareMode(TIMER_D1_BASE, &initComp0Param);
156 
157  Timer_D_initCompareModeParam initComp1Param = {0};
158  initComp1Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_1;
159  initComp1Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
160  initComp1Param.compareOutputMode = TIMER_D_OUTPUTMODE_RESET_SET;
161  initComp1Param.compareValue = 300;
162  Timer_D_initCompareMode(TIMER_D1_BASE, &initComp1Param);
163 
164  Timer_D_initCompareModeParam initComp2Param = {0};
165  initComp2Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_2;
166  initComp2Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
167  initComp2Param.compareOutputMode = TIMER_D_OUTPUTMODE_RESET_SET;
168  initComp2Param.compareValue = 900;
169  Timer_D_initCompareMode(TIMER_D1_BASE, &initComp2Param);
170 
171  Timer_D_startCounter(TIMER_D1_BASE,
172  TIMER_D_UP_MODE);
173 
174  // Configure TD1 TEC External Clear
175  // Need to physically connect P2.0/TD0.2 to P2.7/TEC1CLR
176  GPIO_setAsPeripheralModuleFunctionInputPin(
177  GPIO_PORT_P2,
178  GPIO_PIN7
179  );
180 
181  // High Level trigger, ext clear enable
182  TEC_initExternalClearInput(TEC1_BASE,
183  TEC_EXTERNAL_CLEAR_SIGNALTYPE_LEVEL_SENSITIVE,
184  TEC_EXTERNAL_CLEAR_SIGNAL_NOT_HELD,
185  TEC_EXTERNAL_CLEAR_POLARITY_RISING_EDGE_OR_HIGH_LEVEL
186  );
187  TEC_enableExternalClearInput(TEC1_BASE);
188 
189 
190  __bis_SR_register(LPM0_bits); // Enter LPM0
191  __no_operation(); // For debugger
192 
193 }
194 
195 
196 
__no_operation()
#define UCS_MCLK_FLLREF_RATIO
void main(void)
#define UCS_MCLK_DESIRED_FREQUENCY_IN_KHZ